<?php

namespace app\repository\dao\api;

use support\Db;

class AnalysisDao
{
    public function getList(array &$data): array
    {
        $fields = [
            'id',
            'orgz_id',
            'pos_uuid',
            'sales_code',
            'upload_code',
            'product_id',
            'product_alias',
            'product_price',
            'total_price',
            'product_quantity',
            'unit_name',
            'weight_type',
            'operated_time',
            'created_at',
            'is_error',
        ];
        $query = Db::table('pos_product_weighing_record')
            ->when($data['orgz_id'], fn($query) => $query->where('orgz_id', $data['orgz_id']))
            ->when($data['begin_date'], function ($query) use (&$data) {
                return $query->where('operated_time', '>=', $data['begin_date']);
            })
            ->when($data['end_date'], function ($query) use (&$data) {
                return $query->where('operated_time', '<=', $data['end_date']);
            });

        $total = $query->count();
        $list = $query->offset($data['start'])
            ->limit($data['length'])
            ->orderBy('id', 'DESC')
            ->select($fields)
            ->get()->toArray();
        return ['total' => $total, 'list' => $list];
    }
}